package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domin.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherCaotroller {
    private static TeacherService teacherService=new TeacherService();
    private static Scanner sc=new Scanner(System.in);

    //入口
    public void Teacherstart(){

        teacherloop:
        while (true){
            System.out.println("--------欢迎来到<老师>管理系统-------");
            System.out.println("请输入您的选择：1 添加老师 2 删除老师 3 修改老师 4 查看老师 5 退出系统 ");
            String choice = sc.next();
            switch (choice){
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用，再见！");
                    break teacherloop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }
//查看老师
    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers==null){
            System.out.println("没有数据，请先添加");
        }
        System.out.println("工号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
          if (teacher!=null){
              System.out.println(teacher.getSid()+teacher.getName()+teacher.getAge()+teacher.getBirthday());
          }
        }
    }
//修改老师
    private void updateTeacher() {
        String sid = lurugonghao();
        Teacher teacher = luruTeacherInfo(sid);
        teacherService.updateTeacher(sid,teacher);
        System.out.println("修改成功");
    }
//删除老师
    private void deleteTeacher() {
        String sid = lurugonghao();
        teacherService.deleteTeacher(sid);
        System.out.println("删除成功");
    }
//录入工号 删除和修改
    private String lurugonghao() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exists = teacherService.isExists(sid);
            if (exists) {
                break;
            } else {
                System.out.println("已经存在，请重新输入");
            }
        }
        return sid;
    }

    //添加老师
    private void addTeacher() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exists = teacherService.isExists(sid);
            if (exists) {
                System.out.println("已经存在，请重新输入");
            } else {
                break;
            }
        }
        Teacher teacher = luruTeacherInfo(sid);
        boolean b = teacherService.addTeacher(teacher);
        if (b){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
//录入老师信息，新增和修改
    private Teacher luruTeacherInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
